import Util from '../../../utils/utils';
import {
  HomeHttp
} from '../../../api/home.js'
const homeHttp = new HomeHttp();
Page({
  /**
   * 页面的初始数据
   */
  payComponentEl: null,
  initStatus: false,
  data: {
    goodsList: [],
    page: 1,
    pageSize: 10,
    totalNum: 0,

    activeType: "1",
    orderStatus: 1,
    newTime: 0,
    orderId: false,
    isShowPayBox: false, //支付框
  },
  initPage: function(options) {
    if (options.hasOwnProperty('status')) {
      this.setData({
        activeType: options['status'],
        orderStatus: options['status'],
      });
      console.log(this.data.activeType);
    }
    this.setData({
      newTime: parseInt(new Date().getTime())
    })
    this.httpGetOrderList()
    this.payComponentEl = this.selectComponent('#payBox');
  },

  //获取订单列表
  httpGetOrderList() {
    let {
      goodsList,
      page,
      pageSize,
      orderStatus
    } = this.data;
    homeHttp.getOrderList({
      'pageNum': page,
      'pageSize': pageSize,
      'orderMainType': 3,// 订单主类型:1.门店订单，2.奖品订单，3秒杀订单,默认为1
      // 订单类型(orderStatus):
      // >>> orderMainType=1 时: 0:全部订单(默认),1:待付款,2:待收货,3:已收货,4:退货/退款
      // >>> orderMainType=2 时: 3:已完成,4:未完成
      // >>> orderMainType=3 时: 1:待付款,2:待发货,3:已完成
      'orderStatus': orderStatus
    }).then((res) => {
      wx.stopPullDownRefresh()
      let list = res.spike_order_list
      this.setData({
        goodsList: page === 1 ? list : goodsList.concat(list),
        totalNum: res.total
      })
    })
  },

  // 页面相关事件处理函数--监听用户下拉动作
  onPullDownRefresh: function() {
    this.setData({
      page: 1
    })
    this.httpGetOrderList()
  },

  // 页面上拉触底事件的处理函数：获取推荐商品数据 或者 获取附近店铺数据
  // 上拉刷新
  onReachBottom: function() {
    const { goodsList, totalNum } = this.data
    if (goodsList.length < totalNum) {
      this.setData({
        page: this.data.page + 1
      });
      this.httpGetOrderList()
    }
  },
  //点击顶部tabs
  onTabClick(e) {
    let status = e.detail.name;
    this.setData({
      page: 1,
      orderStatus: status
    });
    this.httpGetOrderList()
  },
  //点击支付按钮
  onPayBtn(e) {
    this.openPayBox(e.detail.orderId);
  },

  //打开支付的盒子
  openPayBox: function (order_id) {
    this.setData({
      orderId: order_id,
      isShowPayBox: true,
    });
    this.payComponentEl.switchPayment(this.data.paymentMethod);
    this.payComponentEl.setShowPayTime(false);
    this.payComponentEl.setOrderID(order_id);
    this.payComponentEl.openPayMethod();
  },

  payComplete: function (res) {
    let self = this;
    self.setData({
      isShowPayBox: false,
    });
    if (res.detail.status === 'complete') {
      let url = '';
      url = '/pages/goods/paySuccess/paySuccess?id=' + self.data.orderId;
      setTimeout(function () {
        Util.toPage(url, false, true);
      }, 300);
    }
    this.httpGetOrderList()
  },

});
